﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient; 
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         
    }
    protected void btnlogin_Click(object sender, EventArgs e)
    {
        string userName = txtuser.Text.ToString().Trim();
        string userPwd = txtpwd.Text.ToString().Trim();
        string userRole = rblClass.SelectedValue.Trim();
        string selectStr = "";
        switch (userRole)
        {
            case "0":
                selectStr = "select * from managers where id='"+userName +"'";
            break;
            case "1":
                selectStr = "select * from sudents where id = '"+userName +"'";
            break ;
        }
        string connectionstring = WebConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
        SqlConnection conn = new SqlConnection(connectionstring);
        //SqlConnection conn = new SqlConnection(ConfigurationManager
        //     .ConnectionStrings["ConnectionString"].ConnectionString);
        //SqlConnection conn = new SqlConnection("server=WANGJIAN-PC;uid=sa;pwd=;database=sales");
        //SqlConnection conn = new SqlConnection("Server=.;database=sales;integrated security=true");
        SqlCommand cmd = new SqlCommand(selectStr, conn);
        conn.Open();
       try
       {
           conn.Open();
           SqlDataReader sdr = cmd.ExecuteReader();
           if (sdr.Read())
           {
               if (sdr.GetString(1) == userPwd)
               {
                   Session["username"] = userName;
                   Session["userrole"] = userRole;
                   conn.Close();
                   switch (userRole)
                   {
                       case "0":
                           Response.Redirect("MRepairsUnFinish.aspx");
                           break;
                       case "1":
                           Response.Redirect("SBasicInformation.aspx");
                           break;
                   }
               }
               else
               { }
           }
           else
           { }
           }
      catch (Exception ee)
      {
         Response .Write ("<script language = javescript>alert('" + 
              ee.Message .ToString() + "')</script>");
      }
      finally 
            {
                conn.Close();
            }

    }
    protected void rblClass_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
